
package Terror;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.util.Random;

/**
 * @author Maria Fernanda Martinez, Adrian Martinez, Darwin Rivas
 */
public class StartMIDPCanvas extends GameCanvas implements CommandListener, Runnable {
    int seleccion;
    Thread hilo;
    Graphics g;
    Sprite inicio, selec;
    TerrMIDlet midletAnterior;
    boolean instruccion=false;
    int tmp=0;
    boolean pag1 = true;
    
    /**
     * Nombre: StartMIDPCanvas
     * Descripcion: constructor de la clase
     * Pre: midletAnterior existe
     * Post: canvas construido
     * No hay valor de retorno
     * @param midletAnterior
     */
    public StartMIDPCanvas(TerrMIDlet midletAnterior) {
        super(true);
        g=getGraphics();
        this.midletAnterior = midletAnterior;
        try {
            // Set up this canvas to listen to command events
            inicio = new Sprite(Image.createImage("/Graphics/screamer4.png"),210,160); 
            selec  = new Sprite(Image.createImage("/Graphics/selec.png"),120,31);
            setCommandListener(this);
            // Add the Exit command
            addCommand(new Command("Exit", Command.EXIT, 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        hilo=new Thread(this);
        hilo.start();
    }

    /**
     * Nombre: paint
     * Descripción: llama a la imprasion del layer y luego al de los objetos (sprites)
     * Pre: no hay
     * Post: no hay
     * No hay parámetros ni valor de retorno
     * @throws InterruptedException 
     */
    public void paint() throws InterruptedException {
        paintLayer();
        paintSprite();
    }
       /**
     * Nombre: getCoorX
     * Descripción: acceder al valor de x
     * Pre: x >= 0
     * Post: regresa valor de x
     * @param x de tipo int
     * @return int
     */
    public int getCoorX(int x){
        return(x*getWidth())/200;
    }
    
    /**
     * Nombre: getCoorY
     * Descripción: acceder al valor de y
     * Pre: y >= 0
     * Post: regresa el valor de y
     * @param y de tipo int
     * @return int
     */
    public int getCoorY(int y){
        return (y*getHeight())/200;
    }
    /**
     * Nombre: paintLayer
     * Descripción:Imprime un fondo de color en la pantalla
     * Pre: no hay
     * Post: no hay
     * No hay valor de retorno ni parámetros
     */
    public void paintLayer(){
        g.setColor(0x3333CC);
        g.fillRect(getCoorX(0), getCoorY(0), getCoorX(200), getCoorY(200));
        
    }
    
    /**
     * Nombre: paintSprite
     * Descripción: despliega en pantalla las imagenes
     * Pre: existen los Sprites
     * Post: Sprites en pantalla
     * No hay valor de retorno ni parámetros
     * @throws InterruptedException 
     */
    public void paintSprite() throws InterruptedException{
        g.setColor(0xFFFFFF);
        if(tmp==0){
            g.drawString("Jugar mapa 1", 60, 80, Graphics.TOP | Graphics.LEFT);
            g.drawString("Jugar mapa 2", 60, 95, Graphics.TOP | Graphics.LEFT);
            g.drawString("Instrucciones", 60, 110, Graphics.TOP | Graphics.LEFT);
            g.drawString("English/Español", 60, 125, Graphics.TOP | Graphics.LEFT);
            g.drawString("Salir", 60, 140, Graphics.TOP | Graphics.LEFT);
        }
        else{
            g.drawString("Play map 1", 60, 80, Graphics.TOP | Graphics.LEFT);
            g.drawString("Play map 2", 60, 95, Graphics.TOP | Graphics.LEFT);
            g.drawString("Instructions", 60, 110, Graphics.TOP | Graphics.LEFT);
            g.drawString("English/Español", 60, 125, Graphics.TOP | Graphics.LEFT);
            g.drawString("Exit", 60, 140, Graphics.TOP | Graphics.LEFT);
        }
        if(instruccion){
            if(pag1){
                instrucciones();
                flushGraphics();
                hilo.sleep(15000);
                pag1 = false;
            }
            else{
                instrucciones2();
            }
        }
        else{
            pag1 = true;
            switch(seleccion){
                case 0:
                    selec.setPosition(50,75);
                    selec.paint(g);
                    flushGraphics();
                    break;
                case 1:
                    selec.setPosition(50,90);
                    selec.paint(g);
                    flushGraphics();
                    break;
                case 2:
                    selec.setPosition(50,105);
                    selec.paint(g);
                    flushGraphics();
                    break;
                case 3:
                    selec.setPosition(50,120);
                    selec.paint(g);
                    flushGraphics();
                    break;
                case 4:
                    selec.setPosition(50, 135);
                    selec.paint(g);
                    flushGraphics();
                    break;
            }
        }
        flushGraphics();
        hilo.sleep(100);
    }
    
    /**
     * Nombre: instrucciones
     * Descripción: despliegue de instrucciones del juego
     * Pre: no hay
     * Post: instrucciones desplegadas
     * No hay valor de retorno ni parámetros
     */
    public void instrucciones(){
        paintLayer();
        g.setColor(0xFFFFFF);
        if(tmp==0){
            g.drawString("Instrucciones del juego", 0 , 0, Graphics.TOP | Graphics.LEFT);
            g.drawString("Estás atrapado en una habitación. Oyes", 0 , 15, Graphics.TOP | Graphics.LEFT);
            g.drawString("gritos extraños. Salir de ahí es tu  ", 0 , 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("prioridad. Debes buscar la llave para ",0, 45, Graphics.TOP | Graphics.LEFT);
            g.drawString("abrir la puerta. Utiliza las teclas de ",0, 60, Graphics.TOP | Graphics.LEFT);
            g.drawString("navegacion para moverte y centro para ",0, 75, Graphics.TOP | Graphics.LEFT);
            g.drawString("seleccionar/buscar. Debes tener cuidado",0, 90, Graphics.TOP | Graphics.LEFT);
            g.drawString("de los fantasmas, los cuales cambian la",0, 105, Graphics.TOP | Graphics.LEFT);
            g.drawString("llave de lugar cada vez que buscas y no",0, 120, Graphics.TOP | Graphics.LEFT);
            g.drawString("encuentras. Ellos también pueden atraparte",0, 135, Graphics.TOP | Graphics.LEFT);           
        }
        else if(tmp==1){
            g.drawString("Instructions of the game", 0 , 0, Graphics.TOP | Graphics.LEFT);
            g.drawString("You're trapped in a room. You listen", 0 , 15, Graphics.TOP | Graphics.LEFT);
            g.drawString("weird screams. Getting out is your ", 0 , 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("priority. You should search for the",0, 45, Graphics.TOP | Graphics.LEFT);
            g.drawString("key to unlock the door. Use the ",0, 60, Graphics.TOP | Graphics.LEFT);
            g.drawString("navigation keys to move and center to",0, 75, Graphics.TOP | Graphics.LEFT);
            g.drawString("select/search. Be aware of the ghosts,",0, 90, Graphics.TOP | Graphics.LEFT);
            g.drawString("who hide the key in a different place",0, 105, Graphics.TOP | Graphics.LEFT);
            g.drawString("every time you seek and don't ",0, 120, Graphics.TOP | Graphics.LEFT);
        }
    }
    
    public void instrucciones2(){
        paintLayer();
        g.setColor(0xFFFFFF);
        if(tmp==0){                                           
            g.drawString("y dejarte inmóvil. Presiona alt t", 0 , 15, Graphics.TOP | Graphics.LEFT);
            g.drawString("para liberarte. También puedes usar", 0 , 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("la opcion de Script. Aquí ingresas", 0, 45, Graphics.TOP | Graphics.LEFT);
            g.drawString("una secuencia de caracteres que se", 0, 60, Graphics.TOP | Graphics.LEFT);
            g.drawString("traducen en movimiento: 'a' izquierda,", 0, 75, Graphics.TOP | Graphics.LEFT);
            g.drawString("'s' abajo, 'w' arriba, 'd' derecha y ", 0, 90, Graphics.TOP | Graphics.LEFT);
            g.drawString("'c' seleccionar. Presiona centro para", 0, 105, Graphics.TOP | Graphics.LEFT);
            g.drawString("regresar al menú principal.     ", 0, 120, Graphics.TOP | Graphics.LEFT);
        }
        else if(tmp==1){
            g.drawString("find. They can also trap you and leave", 0 , 0, Graphics.TOP | Graphics.LEFT);
            g.drawString("you immobilized. Press alt t to get free", 0 , 15, Graphics.TOP | Graphics.LEFT);
            g.drawString("You can algo use the Script option. Here", 0 , 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("you enter a string that is translated to",0, 45, Graphics.TOP | Graphics.LEFT);
            g.drawString("movement: 'a' left, 's' down, 'w' up,",0, 60, Graphics.TOP | Graphics.LEFT);
            g.drawString("'d' right and 'c' select.",0, 75, Graphics.TOP | Graphics.LEFT);
            g.drawString("Press center to go back to main menu",0, 90, Graphics.TOP | Graphics.LEFT);
        }
    }
    
    
    
    
    /**
     * Nombre: eventoTecla
     * Descripción: revisa cual es la opcion seleccionada y la ejecuta
     * Pre: no hay
     * Post: opción ejecutada
     * No hay valor de retorno ni parámetros
     */
     public void eventoTecla(){
         int tecla = getKeyStates();
         switch(tecla){
             case 0:
                 break;
                 
             case UP_PRESSED:
                 seleccion--;
                 if(seleccion<0) 
                     seleccion=4;
                 break;
                
             case DOWN_PRESSED:
                 seleccion++;    
                 if(seleccion>4)
                     seleccion=0;                    
                 break;
             
             case GAME_A_PRESSED:
                 break;
                 
             case 256:
                 if(seleccion==0){
                     midletAnterior.canvas.iniciar();
                     Display.getDisplay(midletAnterior).setCurrent(midletAnterior.canvas);
                 }
                 if(seleccion==1){                     
                     midletAnterior.canvas1.iniciar();
                     Display.getDisplay(midletAnterior).setCurrent(midletAnterior.canvas1);
                 }
                 if(seleccion==2){
                     if(instruccion)
                         instruccion=false;
                     else
                         instruccion=true;
                 }
                 if(seleccion==3){
                     if(tmp==0){
                         midletAnterior.canvas.ingles=true;
                         tmp=1;
                     }
                     else if(tmp==1){
                         midletAnterior.canvas.ingles=false;
                         tmp=0;
                     }
                 }
                 if(seleccion==4){
                     midletAnterior.destroyApp(true);
                     midletAnterior.notifyDestroyed();
                 }
                 break;   
         }
                                  
     }
     
     /**
      * Nombre: run
      * Descripción:
      * Pre: no hay
      * Post: programa en ejecución
      * No hay valor de retorno ni parámetros
      */
     public void run(){
         while(true){
             try {   
                 paint();
                 eventoTecla();
             } catch (InterruptedException ex) {
                 ex.printStackTrace();
             }
         }           
     }
     
     /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
        
    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Nombre: commandAction
     * Descripción: ejecuta la accion de salir
     * Pre: no hay
     * Post: juego terminado
     * @param command
     * @param displayable  
     */
    public void commandAction(Command command, Displayable displayable) {
        if(command.getCommandType() == Command.EXIT){
            midletAnterior.destroyApp(true);
            midletAnterior.notifyDestroyed();
        }
    }
}
